#This is a script showing a number of examples of making # box and whisker plots in R. We will use the same # data that we used for the histograms. # we want to generate the data in the given table source("../gnrnd4.R") gnrnd4(1497539102, 78301453) # as usual, verify that we have the right values head( L1 ) # by default this shows us the first 6 values tail( L1 ) # by default this shows us the last 6 values # then to make a box plot we just use the # boxplot() function boxplot( L1 ) # That plot came out in a vertical format. Let us # switch that to a horizontal format boxplot( L1, horizontal = TRUE) # That is enough for the requirements of this course # but as we have seen before, it might be nice to # fix this up a bit. # Caution: because the default orientation is # vertical, to change the scale in this horizontal # orientation we will use the ylim parameter. boxplot( L1, horizontal = TRUE, ylim=c(140,240), main="Data from the first table on the background sheet.", xlab = "data values given in table 1" ) abline(v=seq(140,240,10), lty="dotted", col="darkgray") # That made it easier to get approximate values. # And do this once more with a finer scale # and with even more info # first use summary to get the values highlighted # in the box plot s_hold <- summary(L1) # Then create a bottom label b_lab <- paste("min=", s_hold[1], " Q1=", s_hold[2], " Med=",s_hold[3], " Q3=", s_hold[5], " max=", s_hold[6]) boxplot( L1, horizontal = TRUE, ylim=c(140,240), main="Data from the first table on the background sheet.", xlab =b_lab, xaxp=c(140,240,20) ) abline(v=seq(140,240,5), lty="dotted", col="darkgray") # # Now look at table 2 # gnrnd4(723859503, 7800145) L1 boxplot(L1, horizontal=TRUE) # or, again, we can fix this up s_hold <- summary(L1) # Then create a bottom label b_lab <- paste("min=", s_hold[1], " Q1=", s_hold[2], " Med=",s_hold[3], " Q3=", s_hold[5], " max=", s_hold[6]) boxplot( L1, horizontal = TRUE, ylim=c(140,240), main="Data from the second table on the background sheet.", xlab =b_lab, xaxp=c(140,240,20) ) abline(v=seq(140,240,5), lty="dotted", col="darkgray") # # Now look at table 3 # gnrnd4(1849829701, 79001403) L1 boxplot( L1, horizontal = TRUE) # or, again, we can fix this up s_hold <- summary(L1) # Then create a bottom label b_lab <- paste("min=", s_hold[1], " Q1=", s_hold[2], " Med=",s_hold[3], " Q3=", s_hold[5], " max=", s_hold[6]) boxplot( L1, horizontal = TRUE, ylim=c(140,220), main="Data from the third table on the background sheet.", xlab =b_lab, xaxp=c(140,220,20) ) abline(v=seq(140,220,5), lty="dotted", col="darkgray") # # Now look at table 4 # gnrnd4(1849829704, 18001800) L1 boxplot(L1, horizontal=TRUE) # or, again, we can fix this up s_hold <- summary(L1) # Then create a bottom label b_lab <- paste("min=", s_hold[1], " Q1=", s_hold[2], " Med=",s_hold[3], " Q3=", s_hold[5], " max=", s_hold[6]) boxplot( L1, horizontal = TRUE, ylim=c(140,220), main="Data from the fourth table on the background sheet.", xlab =b_lab, xaxp=c(140,220,16) ) abline(v=seq(140,220,5), lty="dotted", col="darkgray") # # A few more steps may help us see how close this is # to a normal distribution. If it is normal then we # expect about 25% of the values to be less than the # mean - 0.675*sd and about 25% of the values to be # greater than mean + 0.675*sd, and the mean and # median should be about the same. Let us find and # graph those three values source("../pop_sd.R") sd_hold = pop_sd( L1 ) mu <- mean( L1 ) low_val <- mu - 0.675*sd_hold high_val <- mu + 0.675*sd_hold abline( v=c(low_val, mu, high_val), col="red", lty="dashed") # # Now look at table 5 # # this time we are using gnrnd5 so load that # function into our environment source("../gnrnd5.R") gnrnd5(143879025405, 124001500, 134002100) head(L1) tail(L1) boxplot(L1, horizontal = TRUE) # or, again, we can fix this up s_hold <- summary(L1) # Then create a bottom label b_lab <- paste("min=", s_hold[1], " Q1=", s_hold[2], " Med=",s_hold[3], " Q3=", s_hold[5], " max=", s_hold[6]) boxplot( L1, horizontal = TRUE, ylim=c(100,260), main="Data from the fifth table on the background sheet.", xlab =b_lab, xaxp=c(100,260,16) ) abline(v=seq(100,260,10), lty="dotted", col="darkgray")